package com.lession.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lession.Dto.GetLessonsReqDto;
import com.lession.mapper.CoursesMapper;
import com.lession.mapper.LessonMapper;
import com.lession.mapper.ScheduleMapper;
import com.lession.mapper.WeekTableMapper;
import com.lession.pojo.COURSES;
import com.lession.pojo.LESSONBYWEEK;
import com.lession.pojo.SCHEDULE;
import com.lession.pojo.WEEK_TABLE;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class LessonService {
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private ScheduleMapper scheduleMapper;
	@Autowired
	private LessonMapper lessonMapper;
	@Autowired
	WeekTableMapper weekTableMapper;
	@Autowired
	private CoursesMapper coursesMapper;
	
	/**
	 * @Description: 根据学生 周获取课表
	 */
	public JSONObject getLessonsByStu(GetLessonsReqDto dto) {
		JSONObject result = new JSONObject();
		
		//获取上课时间参照
		List<SCHEDULE> schedules = scheduleMapper.select();
		int weekNumbers = 1;//一共显示几周
		
		String stu_id = dto.getStu_id();
		int controlWeek = dto.getControlWeek();
		JSONArray lessonInfos = new JSONArray();
		if (StringUtils.isNotBlank(stu_id)) {
			List<WEEK_TABLE> week_tables = weekTableMapper.select();
			LinkedHashMap<String, List<Object>> week_map = new LinkedHashMap();
			if (!week_tables.isEmpty()) {
				for (WEEK_TABLE week_table : week_tables) {
					List<Object> list = new ArrayList<>();
					for (int i = 0; i < schedules.size(); i++) {
						list.add(null);
					}
					week_map.put(week_table.getWeek_id(), list);
				}
				List<LESSONBYWEEK> lessonByWeek = lessonMapper.getLessonByWeek(stu_id, controlWeek, null);
				for (LESSONBYWEEK lessonbyweek : lessonByWeek) {
					week_map.get(lessonbyweek.getWeek_id()).set(Integer.valueOf(lessonbyweek.getCourse_num()) - 1, lessonbyweek);
				}
			}
			System.out.println(week_map);
			for (List<Object> value : week_map.values()) {
				lessonInfos.add(value);
			}
		}
		result.put("schedules", schedules);
		result.put("lessonInfos", lessonInfos);
		result.put("weekNumbers", weekNumbers);
		return result;
	}
	
	public JSONArray selectCourses() {
		JSONArray result = new JSONArray();
		List<COURSES> select = coursesMapper.select();
		for (COURSES courses : select) {
			JSONObject obj = new JSONObject();
			obj.put("value", courses.getCourse_id());
			obj.put("text", courses.getCourse_name());
			result.add(obj);
		}
		return result;
	}
	
	/**
	 * @Description: 根据学生 几周 星期七几获取课表
	 */
	public List<LESSONBYWEEK> getlessonByday(GetLessonsReqDto dto) {
		List<LESSONBYWEEK> lessonByWeek = new ArrayList<>(0);
		String stu_id = dto.getStu_id();
		int controlWeek = dto.getControlWeek();
		String week_id = dto.getWeek_id();
		if (StringUtils.isNotBlank(stu_id) && StringUtils.isNotBlank(stu_id)) {
			lessonByWeek = lessonMapper.getLessonByWeek(stu_id, controlWeek, week_id);
		}
		return lessonByWeek;
	}
	
}
